/*
 * Copyright (c) 2009 Sun Microsystems, Inc.
 * Copyright (c) 2011 Oracle Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **/

package com.sun.spot.wot.gateway;


import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.spot.wot.http.HttpResponse;


/**
 *
 * @author vgupta
 */
public abstract class StreamForwarder implements DeviceForwarder {
    protected InputStream is = null; 
    protected OutputStream os = null;

    public abstract void openConn(String addr);
    public abstract void closeConn();

    public void close() {
        closeConn();
    }
    
    public int sendAndGetResponse(String addr,
            byte[] sndBuf, int off1, int len1, byte[] rcvBuf, int off2) {
        int cnt = 0;
        try {
//            System.out.println("Connecting to " + addr);
            openConn(addr);

            int tmp = 0;
            int off = off2;


            // First send the data
            os.write(sndBuf, off1, len1);
            //os.write(0); // XXX Won't work for compression
            os.flush();
            //System.out.println("Sent data [len=" + len1 + "]:\n" +
            //        PrettyPrint.prettyPrint(sndBuf, off1, len1));

            HttpResponse resp = new HttpResponse();
            resp.parse(is);
            cnt = resp.toByteArray(true, rcvBuf, off2);
//            System.out.println("StreamForwarder received: \n" +
//                    PrettyPrint.prettyPrint(rcvBuf, off2, cnt));
//            System.out.println("Reading data, off=" + off);
//            while ((tmp = is.read()) !=  -1) {
//                System.out.println("Got " + (char) tmp);
//                rcvBuf[off++] = (byte) (tmp & 0xff);
//                cnt++;
//            }
        } catch (Exception ex) {
            Logger.getLogger(StreamForwarder.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (Exception ex) {
                Logger.getLogger(StreamForwarder.class.getName()).log(Level.SEVERE, null, ex);
            }
            closeConn();
        }

        return cnt;
    }
}

